package com.longrise.android.byjk.plugins.dealsituation.course.video2.study;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.StudyRecordBase;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

/**
 * Created by godliness on 2017/9/2.
 */

public class LoopRecord {

    private static final String TAG = "LoopRecord";

    /**
     * 过程维护
     */
    public static void loop(StudyRecordBase.Record record, final DefendHandler.OnDefend onDefend) {
        if (record == null || onDefend == null) {
            PrintLog.e(TAG, "loop == null");
            return;
        }
        EntityBean bean = new EntityBean();
        bean.set("duration", record.mDuration);
        bean.set("position", record.mPosition);
        bean.set("recordid", record.mId);
        bean.set("studentno", record.mStudentno);
        bean.set("cwid", record.mCwid);
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("comfrom", "Android");
        EntityBean recordBeans = new EntityBean();
        recordBeans.set("bean", bean);

        LoadDataManager.getInstance()
                .callService(null, UrlConstants.BaseUrl, "bbt_course_uStudyProcessRecord", recordBeans, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                EntityBean resultBean = (EntityBean) result;

                boolean isvideopass = false;
                int resultstate = 0;

                if (resultBean != null) {
                    resultstate = resultBean.getInt("resultstate");
                    EntityBean resultStr = resultBean.getBean("result");
                    if (resultStr != null) {
                        //获取状态值 isvideopass  position
                        isvideopass = resultStr.getBoolean("isvideopass");
                    }
                }

                //按照resultstate的值做处理，如果resultstate != 1 执行onDefend,否则onError()
                if (resultstate == 1) {
                    if (onDefend != null) {
                        onDefend.onDefend(isvideopass);
                    }
                } else {
                    if (onDefend != null) {
                        onDefend.onError();
                    }
                }
                PrintLog.e(TAG, "onSuccess desc: " + resultBean.getString("resultdesc"));
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                if (onDefend != null) {
                    onDefend.onError();
                }
            }

            @Override
            public void onFinished(String key, String service) {

            }
        });
    }
}
